﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Red5.Frame.Utility.DataHandling.Encryption.V1
{
    public class Handling : IEncryption
    {
        private string key;
        private string vector;

        public Handling(string key, string vector)
        {
            this.key = key;
            this.vector = vector;
        }

        public void EncryptData(byte[] data)
        {
            // 这里需要实现具体的加密逻辑
            // 使用 AES 加密
            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(key);
                aes.IV = Encoding.UTF8.GetBytes(vector);
                aes.Padding = PaddingMode.PKCS7;

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                    }
                    Data = ms.ToArray();
                }
            }
        }
        public void DecryptData(byte[] data)
        {
            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(key);
                aes.IV = Encoding.UTF8.GetBytes(vector);
                aes.Padding = PaddingMode.PKCS7;

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                    }
                    Data = ms.ToArray();
                }
            }
        }
        public byte[] Data { get; set; }
    }
}
